package com.huli.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huli.mapper.RoleMapper;
import com.huli.pojo.SysRole;
import com.huli.service.RoleService;
import com.huli.util.Result;
import com.huli.vo.OrganizationUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    // 注入RoleMapper
    @Autowired
    private RoleMapper roleMapper;
    // 根据分页参数查询角色列表
    @Override
    public OrganizationUserVo roleList(OrganizationUserVo organizationUserVo) {
        // 设置分页参数
        PageHelper.startPage(organizationUserVo.getPageNum(), organizationUserVo.getPageSize());
        // 查询角色列表
        List<SysRole> sysRoles = roleMapper.roleList(organizationUserVo);
        // 将查询结果封装到PageInfo中
        PageInfo<SysRole> sysRolePageInfo = new PageInfo<>(sysRoles);
        // 返回封装后的结果
        return new OrganizationUserVo(sysRolePageInfo.getTotal(), sysRolePageInfo.getList());
    }

    // 根据角色名称查询角色
    @Override
    public List<SysRole> roleName(String roleName) {
        return roleMapper.roleName(roleName);
    }

    // 更新角色信息
    @Override
    public void updateRole(SysRole sysRole) {
        // 设置更新时间
        sysRole.setUpdateTime(new Date());
        // 更新角色信息
        roleMapper.updateRole(sysRole);
    }

    // 添加角色
    @Override
    public void addRole(SysRole sysRole) {
        // 添加角色
        roleMapper.addRole(sysRole);
    }

    @Override
    public SysRole selectRoleById(Long id) {
        SysRole sysRole = roleMapper.selectRoleById(id);
        return sysRole;
    }

    @Override
    public void deleteRole(Long id) {
        roleMapper.deleteRole(id);
    }
}
